package tree.test107;

import listbyorder.utils.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/7/16 12:24
 */
public class Solution1 {

    // DFS(深度优先遍历)
    List<List<Integer>> res = new ArrayList<>();

    public List<List<Integer>> levelOrderBottom(TreeNode root) {
        dfs(root, 0);
        return res;
    }

    private void dfs(TreeNode root, int l) {
        if (root == null) return;
        if (l >= res.size()) {
            res.add(0, new ArrayList<>());
        }
        res.get(res.size() - l - 1).add(root.val);
        dfs(root.left, l + 1);
        dfs(root.right, l + 1);
    }
}
